package com.example.gray.controller;

import com.example.gray.entity.OpLog;
import com.example.gray.entity.Result;
import com.example.gray.service.OpLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Gray 2021/12/28-18:28
 */
@RestController
@CrossOrigin
@Api(tags = "日志")
@RequestMapping(value = "log")
public class LogController {

    @Autowired
    private OpLogService logService;

    @GetMapping(value = "select/all")
    @ApiOperation(value = "查询所有日志")
    public Object selectAll(){
        return Result.success(logService.selectAll());
    }

    @GetMapping(value = "select/userId")
    @ApiOperation(value = "查询特定用户日志")
    public Object selectById(Integer id){
        return Result.success(logService.selectById(id));
    }

    @GetMapping(value = "select/time")
    @ApiOperation(value = "查询时间区域日志")
    public Object selectByTime(String S,String E){
        return Result.success(logService.selectByTime(S,E));
    }

    @PostMapping(value = "insert")
    @ApiOperation(value = "插入数据")
    public Object insert(@RequestBody OpLog log){
        if (logService.insert(log) == 1) {
            return Result.success("插入成功");
       }else {
            return Result.error(4001,"插入失败","");
        }
    }

    @PostMapping(value = "delete")
    @ApiOperation(value = "删除数据")
    public Object insert(Integer id){
        if (logService.delete(id) == 1) {
            return Result.success("删除成功");
        }else {
            return Result.error(4001,"删除失败","");
        }
    }

    @PostMapping(value = "update")
    @ApiOperation(value = "更新数据")
    public Object update(@RequestBody OpLog log){
        if (logService.update(log) == 1) {
            return Result.success("更新成功");
        }else {
            return Result.error(4001,"更新失败","");
        }
    }
}
